package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjFactory;
import com.zb.entity.ZjFactoryOrderModuleSettlement;
import com.zb.mapper.ZjFactoryOrderModuleSettlementMapper;
import com.zb.service.ZjFactoryOrderModuleSettlementService;
import com.zb.vo.FactoryOrderModuleSettlementVo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 厂商工单模块结算表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-02-27
 */
@Service
public class ZjFactoryOrderModuleSettlementServiceImpl extends ServiceImpl<ZjFactoryOrderModuleSettlementMapper, ZjFactoryOrderModuleSettlement> implements ZjFactoryOrderModuleSettlementService {

    // 保存商家工单结算
    @Override
    public void save(Long factoryId, List<FactoryOrderModuleSettlementVo> moduleSettlements, List<Long> delModuleSettlementIds) {
        if (CollectionUtil.isNotEmpty(delModuleSettlementIds)) {
            this.removeByIds(delModuleSettlementIds);
        }
        if (CollectionUtil.isEmpty(moduleSettlements)) {
            return;
        }
        QueryWrapper<ZjFactoryOrderModuleSettlement> wrapper = new QueryWrapper<>();
        wrapper.select("id", "module_id")
                .eq("factory_id", factoryId);
        List<ZjFactoryOrderModuleSettlement> settlementList = this.list(wrapper);
        moduleSettlements.forEach(t -> {
            if (null == t.getId()) {
                t.setId(settlementList.stream().filter(f -> Objects.equals(f.getModuleId(), t.getModuleId())).map(ZjFactoryOrderModuleSettlement::getId).findFirst().orElse(null));
            }
        });

        List<ZjFactoryOrderModuleSettlement> adds = new ArrayList<>();
        List<ZjFactoryOrderModuleSettlement> updates = new ArrayList<>();
        ZjFactoryOrderModuleSettlement moduleSettlement = null;
        for (FactoryOrderModuleSettlementVo vo : moduleSettlements) {
            moduleSettlement = new ZjFactoryOrderModuleSettlement();
            BeanUtil.copyProperties(vo, moduleSettlement);
            if (null != moduleSettlement.getId()) {
                updates.add(moduleSettlement);
                continue;
            }
            moduleSettlement.setId(IdWorker.getId());
            moduleSettlement.setFactoryId(factoryId);
            adds.add(moduleSettlement);
        }
        if (CollectionUtil.isNotEmpty(adds)) this.saveBatch(adds);
        if (CollectionUtil.isNotEmpty(updates)) this.updateBatchById(updates);
    }

    // 根据品牌商家ID获取工单结算价
    @Override
    public List<ZjFactoryOrderModuleSettlement> getByFactoryId(Long factoryId) {
        QueryWrapper<ZjFactoryOrderModuleSettlement> wrapper = new QueryWrapper<>();
        wrapper.select("id", "module_id", "price", "is_all_inclusive_fee AS allInclusiveFee")
                .eq("factory_id", factoryId);
        return this.list(wrapper);
    }

    // 根据品牌商家&工单模块ID获取商家结算价
    @Override
    public List<ZjFactoryOrderModuleSettlement> getByFactoryIdsAndModuleIds(List<Long> factoryIds, List<Long> moduleIds) {
        if (CollectionUtil.isEmpty(factoryIds) || CollectionUtil.isEmpty(moduleIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjFactoryOrderModuleSettlement> wrapper = new QueryWrapper<>();
        wrapper.select("factory_id", "module_id", "price", "is_all_inclusive_fee AS allInclusiveFee")
                .in("factory_id", factoryIds)
                .in("module_id", moduleIds);
        return this.list(wrapper);
    }

    // 根据品牌商家&工单模块ID获取商家结算价
    @Override
    public ZjFactoryOrderModuleSettlement getByFactoryIdAndModuleId(Long factoryId, Long moduleId) {
        List<Long> factoryIds = new ArrayList<>();
        List<Long> moduleIds = new ArrayList<>();
        factoryIds.add(factoryId);
        moduleIds.add(moduleId);
        List<ZjFactoryOrderModuleSettlement> settlementList = getByFactoryIdsAndModuleIds(factoryIds, moduleIds);
        return CollectionUtil.isNotEmpty(settlementList) && settlementList.size() > 0? settlementList.get(0) : null;
    }

    // 根据工单模块ID获取品牌商家结算价
    @Override
    public List<ZjFactoryOrderModuleSettlement> getByModuleId(Long moduleId) {
        QueryWrapper<ZjFactoryOrderModuleSettlement> wrapper = new QueryWrapper<>();
        wrapper.select("factory_id", "module_id", "price")
                .eq("module_id", moduleId);
        return this.list(wrapper);
    }
}
